import 'package:equatable/equatable.dart';
import 'package:website.core/core/domain/entities/access_credentials.dart';
import 'package:website.core/core/domain/entities/user.dart';
import 'package:website.core/core/domain/repositories/user_local_repository/repository.dart';

part 'result.dart';

abstract class ObtainAuthenticationData {
  Future<AuthenticationDataObtainmentResult> call();
}

class ObtainAuthenticationDataImpl implements ObtainAuthenticationData {
  const ObtainAuthenticationDataImpl({
    required UserLocalRepository userLocalRepository,
  }) : _userLocalRepository = userLocalRepository;

  final UserLocalRepository _userLocalRepository;

  @override
  Future<AuthenticationDataObtainmentResult> call() async {
    final futureResults = await Future.wait([
      _userLocalRepository.getUser(),
      _userLocalRepository.getAccessCredentials(),
    ]);

    var i = 0;
    final user = futureResults[i++] as User?;
    final accessCredentials = futureResults[i++] as AccessCredentials?;

    final result = AuthenticationDataObtainmentResult(
      accessCredentials: accessCredentials,
      user: user,
    );

    return result;
  }
}
